package com.swkl.yuan.agvtask.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swkl.yuan.agvtask.dao.AgvTaskDao;
import com.swkl.yuan.agvtask.entity.AgvTask;
import com.swkl.yuan.agvtask.service.AgvTaskService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * (AgvTask)表服务实现类
 *
 * @author makejava
 * @since 2024-06-18 16:21:15
 */
@Service("agvTaskService")
public class AgvTaskServiceImpl extends ServiceImpl<AgvTaskDao, AgvTask> implements AgvTaskService {

    @Override
    public List<AgvTask> getAllAgvTask() {
        return list();
    }

    @Override
    public Boolean addOneAgvTask(AgvTask agvTask) {
        return save(agvTask);
    }

    @Override
    public Boolean modifyAgvTask(AgvTask agvTask) {
        return updateById(agvTask);
    }

    @Override
    public Boolean deleteAgvTask(Long id) {
        return removeById(id);
    }

    @Override
    public IPage<AgvTask> getAllAgvTaskByPage(AgvTask agvTask, int page, int limit) {
        LambdaQueryWrapper<AgvTask> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(agvTask.getStartLocation())){
            wrapper.eq(AgvTask::getStartLocation,agvTask.getStartLocation());
        }
        if (ObjectUtil.isNotEmpty(agvTask.getEndLocation())){
            wrapper.eq(AgvTask::getEndLocation,agvTask.getEndLocation());
        }
        if (ObjectUtil.isNotEmpty(agvTask.getState())){
            wrapper.eq(AgvTask::getState,agvTask.getState());
        }
        return page(new Page<>(page, limit), wrapper);
    }

    @Override
    public Boolean batchReplace(List<String> ids, int type, String pram) {
        LambdaUpdateWrapper<AgvTask> updateWrapper = new LambdaUpdateWrapper<>();
        switch (type){
            case 1:
                updateWrapper.set(AgvTask::getPlcMonitorOffset,pram);
                break;
            case 2:
                updateWrapper.set(AgvTask::getPlcFinishOffset,pram);
                break;
            case 3:
                updateWrapper.set(AgvTask::getStartLocation,pram);
                break;
            case 4:
                updateWrapper.set(AgvTask::getEndLocation,pram);
                break;
            case 5:
                updateWrapper.set(AgvTask::getTemplate,pram);
                break;
            default:
                return false;
        }
        updateWrapper.in(AgvTask::getId,ids);
        return update(updateWrapper);
    }
}

